<?php
/**
 * 主页布局
 */
namespace Admin\Controller;
use Think\Controller;
class ProductController extends InitController {

	//产品列表
	public function index(){
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		$where = '';
		$title = I('get.title','','strip_tags,htmlspecialchars,trim');
		$category = I('get.category','','strip_tags,htmlspecialchars,trim');
		
		if (!empty($title)) {
			$where['a.title'] = array('LIKE','%' . $title . '%');
			$this->assign('title',$title);
		}

		if(!empty($category)){
			$where['c.name'] = array('LIKE','%' . $category . '%');
			$this->assign('ProductCategory',$category);
		}

		$start_time = I('get.start_time','','strip_tags,htmlspecialchars,trim');
		$end_time = I('get.end_time','','strip_tags,htmlspecialchars,trim');
	
		if ($start_time !== '') {
			$start_time = strtotime($start_time);
			$this->assign('start_time',$start_time);
		}
		
		if ($end_time !== '') {
			$end_time = strtotime($end_time);
			$this->assign('end_time',$end_time);
		}
		
		if (!empty($start_time) && !empty($end_time)) {
			// 如果有开始及结束时间 则取中值
			$where['a.createtime'] = array(array('EGT', $start_time), array('ELT', $end_time));
		}else{
			if (!empty($start_time)) {
				// 有开始时间
				$where['a.createtime'] = array('EGT', $start_time);
			}elseif (!empty($end_time)) {
				// 有结束时间
				$where['a.createtime'] = array('ELT', $end_time);
			}
		}
		
		$status = I('get.status','','strip_tags,htmlspecialchars,trim');

		if($status !== ''){
			if($status != 'all'){
				$status = $status + 0;
				$where['a.status'] = $status;
			}
		}

		$productModel = D('Product');

		$total = $productModel
				->alias('a')
				->join('LEFT JOIN __PRODUCT_CATEGORY__ as c ON a.cid = c.id')
				->where($where)
				->count();

		$Page = $this->setPageHander($total);
		$show = $Page?$Page->show():'';
		$list = $productModel->getProductList($where,'a.taxis desc,a.id desc',$Page->firstRow.','.$Page->listRows,1);

		$this->assign('status',$status);
		$this->assign('list',$list);
		$this->assign('title',$title);
		$this->assign('category',$category);
		$this->assign('pager',$show);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('index');
		
	}

	//添加产品
	public function add_product(){
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];

		$id = I('id',0,'intval');

		if(!empty($id)){
			$data = D('Product')->getArticleById($id);
			$title = '编辑产品';
		}else{
			$title = '发布产品';
		}

		//产品分类
		$cate = M('ProductCategory')->where("id > 0 and status = 1")->field('id,name,parent_id')->order("taxis desc")->select();

		$this->assign('id',$id);
		$this->assign('data',$data);
		$this->assign(array(
			'defSubTitle' => $title,
			'ajaxUrl' => U('Admin/Product/do_add_product'),
			'data' => $data,	// 默认显示
			'cate' => $cate,
		));
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('productEdit');
	}

	//添加产品动作
	public function do_add_product(){
		if (!IS_AJAX) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'非法操作！','field'=>''));
		}
		$title = I('title','','strip_tags,htmlspecialchars,trim');

		if (empty($title)) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'请输入页面标题！','field'=>''));
		}

		$cid = I('cid',0,'intval');
		$id = I('id',0,'intval');

		if (!$cid) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'请选择产品分类','field'=>''));
		}

		$taxis = I('taxis',100,'intval');
		$thumb = I('thumb','','trim');
		$title = I('title','','trim');
		$uid = session('uid');
		$cid = I('cid','','trim');
		$type = I('type','1','trim');
		$video_url = I('video_url','','trim');
		$url = I('url','','trim');
		$button_position = I('button_position','','trim');
		$button_type = I('button_type','','trim');
		$status = I('status',1,'intval');
		
		
		
		$data['title'] = $title;
		$data['uid'] = session('uid');
		$data['createtime'] = NOW_TIME;
		$data['cid'] = $cid;
		$data['thumb'] = $thumb;
		$data['taxis'] = $taxis;
		$data['type'] = $type;
		$data['video_url'] = $video_url;
		$data['button_position'] = $button_position;
		$data['button_type'] = $button_type;
		$data['status'] = $status;
		$data['url'] = $url;

		if(empty($id)){
			$bool = M('Product')->add($data);
		}else{
			$bool = M('Product')->where(array('id'=>$id))->save($data);
		}
		
		
		if($bool){
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
		} else {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败！','field'=>''));
		}

	}

	public function delAct(){
		if (!IS_AJAX) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'非法操作！','field'=>''));
		}

		$id = I('id');

		if(empty($id)){
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'参数错误','field'=>''));
		}

		$bool = M('Product')->where(array('id'=>$id))->save(array('status'=>'-1'));

		if($bool){
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功','field'=>''));
		}else{
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败','field'=>''));
		}

	}

}
